package home.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.GridLayoutHelper;
import com.bumptech.glide.Glide;

import net.ck.hzq.MyEarningsActivity;
import net.ck.hzq.R;
import net.ck.hzq.ShareActivity;
import net.ck.hzq.TobuyApplication;
import net.ck.hzq.view.LearnMoreActivity;

import java.util.ArrayList;
import java.util.List;

import Bean.HomeTargetBean;
import Utils.ScreenUtil;
import activity.MyJiFenActivity;
import ui.activity.home.AreaAct;
import ui.activity.home.KnowledgeAct;
import ui.activity.home.PickupMachineAct;
import ui.activity.mine.MessageAct;
import ui.activity.poscontrol.PosControlMainAct;
import ui.activity.profit.KF5CustomerAct;
import ui.activity.profit.ProfitAct;
import ui.activity.profit.RankingAct;
import ui.activity.profit.SignAct;
import util.ToastUtils;

/**
 * by BF00233 on 2018/12/10.
 */
public class HomeTargetAdapter extends DelegateAdapter.Adapter<HomeTargetAdapter.HomeTargetVH> {

    private final Context mContext;
    private List<HomeTargetBean> mData;

    public HomeTargetAdapter(Context context) {
        mContext = context;
        mData = new ArrayList<>();
    }

    public void setupData(@NonNull List<HomeTargetBean> data) {
        mData.clear();
        mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        GridLayoutHelper helper = new GridLayoutHelper(4);
        helper.setVGap(ScreenUtil.dip2px(mContext, 20));
        helper.setMargin(0, ScreenUtil.dip2px(mContext, 10),
                0, ScreenUtil.dip2px(mContext, 10));
        return helper;
    }

    @NonNull
    @Override
    public HomeTargetVH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new HomeTargetVH(LayoutInflater.from(mContext).inflate(R.layout.item_home_target, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final HomeTargetVH holder, final int position) {
        if (position < 0 || position >= mData.size()) {
            return;
        }
        HomeTargetBean targetBean = mData.get(position);
        Glide.with(mContext)
                .load(targetBean.iconResId)
                .into(holder.icon);
        holder.title.setText(targetBean.title);

        holder.itemView.setOnClickListener(view -> {
            if (!TobuyApplication.isAuthed()) {
                ToastUtils.shortToast("请先进行实名认证");
                return;
            }

            Intent intent = null;
            // 机具申领
            if (position == 0) {
                intent = new Intent(mContext, PickupMachineAct.class);
            }
            // 终端管理
            if (position == 1) {
                intent = new Intent(mContext, PosControlMainAct.class);
            }
            //积分兑换
            if (position == 2) {
                intent = new Intent(mContext, MyJiFenActivity.class);
            }
            if (position == 3) {
                //在线客服
                intent = new Intent(mContext, KF5CustomerAct.class);

            }
            if (position == 4) {
                //我的收益
                intent = new Intent(mContext, MyEarningsActivity.class);
            }
            if (position == 5) {

                //当日收益
                intent = new Intent(mContext, ProfitAct.class);
            }
            // 业绩排名
            if (position == 6) {
                intent = new Intent(mContext, RankingAct.class);
            }

            //活动专区
            if (position == 7) {

                intent = new Intent(mContext, AreaAct.class);

            }
            // 商学院
            if (position == 8) {
                intent = new Intent(mContext, KnowledgeAct.class);

            }
            // 我要分享
            if (position == 9) {
                intent = new Intent(mContext, ShareActivity.class);
            }
            // 每日签到
            if (position == 10) {
                intent = new Intent(mContext, SignAct.class);
            }

            //了解更多
            if (position == 11) {
                intent = new Intent(mContext, LearnMoreActivity.class);
//                intent = new Intent(mContext, CustomerAct.class);
            }

            if (intent != null) {
                mContext.startActivity(intent);
            } else {
                ToastUtils.shortToast("功能即将开放");
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class HomeTargetVH extends RecyclerView.ViewHolder {
        private ImageView icon;
        private TextView title;

        HomeTargetVH(@NonNull View itemView) {
            super(itemView);
            icon = itemView.findViewById(R.id.icon);
            title = itemView.findViewById(R.id.title);
        }
    }

}
